var UserTask = function() {
	this.init();
}

UserTask.prototype = {
	init: function() {
		var _this = this;
		
		//选择任务类型
		new ModalChoseTable({
			modalSize: "400px",
			id: "taskType",
			dataUrl: "http://112.124.121.34:8091/shihou.forum.service/forum/query?activityId=3149",
			dataParameter: {
				"activityId": "3149"
			},
			itemNameKey: 'content',
			itemIdKey: 'id'
		});
		_this.typeChange();

		//奖励【添加】
		$('.self-share-task-add-btn').on("click", function() {
			if($('section.self-share-task-section .form-group').length > 4)
				return;
			$('section.self-share-task-section').append(_this.shareUserRewardHTML());
			//点击 添加按钮之后，显示奖励类型，金额/次数，方案，等等
			showSelect($('section.self-share-task-section .form-group:last'), 'self-share-task-', 1);
			//渲染select change事件
			$('.self-share-task-select').on("change", function() {
				var value = $(this).val();
				if(value === "4") {
					selfShareScheme();
				}
				showSelect($(this).parent().parent(), 'self-share-task-', value);
			});
			deleteBtn();
		});

		function showSelect(selector, classname, value) {
			for(var i = 1; i < 5; i++) {
				selector.find('.' + classname + i).hide();
			}
			selector.find('.' + classname + value).show();
		}

		function deleteBtn() {
			$('.btn-delete').on("click", function() {
				$(this).parent().parent().remove();
			});
		}

		function selfShareScheme() {
			new ModalChoseTable({
				modalSize: "400px",
				id: "newUserReward",
				dataUrl: "http://112.124.121.34:8091/shihou.forum.service/forum/query",
				dataParameter: {
					"activityId": "3149"
				},
				itemNameKey: 'content',
				itemIdKey: 'id'
			});
		}
	},
	/**
	 * 点击类型radio进行变换
	 */
	typeChange: function(){
		var type = ['new-user-task', 'self-share-task'];
		$('.task-type input[type=radio]').click(function(){
			var value = $(this).val();
			switch(value){
				case "1":
					swichoverShow(type[0]);
					break;
				case "2":
					swichoverShow(type[1]);
					break;
				default:
					break;
			}
		});
		
		function swichoverShow(show){
			//先隐藏，再显示。这样的做法时因为房间活动和全局房间活动公用【弹窗图片】
			type.forEach(function(value){
				$("."+value).hide();
			});
			$("."+show).show();
		}
		
		//初始化时，触发点击【房间活动】radio
		$('.task-type input[type=radio]').eq(0).click();
	},
	shareUserRewardHTML: function(){
		var html = '<div class="form-group">'+
					'	<label class="col-xs-2"></label>'+
					'	<div class="col-xs-1">'+
					'		<select class="form-control self-share-task-select">'+
					'			<option value="1" selected="">狮毛</option>'+
					'			<option value="2">狮牙</option>'+
					'			<option value="3">VIP特权</option>'+
					'			<option value="4">抽奖</option>'+
					'		</select>'+
					'	</div>'+
					'	<label class="col-xs-1">'+
					'		<p class="text-center self-share-task-1 self-share-task-2">金额</p>'+
					'		<p class="text-center self-share-task-3">天数</p>'+
					'		<p class="text-center self-share-task-4">次数</p>'+
					'	</label>'+
					'	<div class="col-xs-1">'+
					'		<input class="form-control" type="number" min="0" />'+
					'	</div>'+
					'	<label class="col-xs-1">'+
					'		<p class="text-center self-share-task-3">类型</p>'+
					'		<p class="text-center self-share-task-4">方案</p>'+
					'	</label>'+
					'	<div class="col-xs-1">'+
					'		<input type="text" class="form-control modal-chose-solution self-share-task-4" id="newUserReward" readonly="readonly" placeholder="点击选择方案" value="" />'+
					'		<select class="form-control self-share-task-3">'+
					'			<option selected="">白银</option>'+
					'			<option>黄金</option>'+
					'			<option>铂金</option>'+
					'		</select>'+
					'	</div>'+
					'	<div class="col-xs-1">'+
					'		<input type="button" class="btn btn-danger btn-block btn-delete" value="删除"></input>'+
					'	</div>'+
					'	<div class="col-xs-1">'+
					'		<button class="btn btn-primary btn-block">保存</button>'+
					'	</div>'+
					'</div>';
		return html;
	}
}

$(document).ready(function(){
	new UserTask();
});
